package com.hugeyurt.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.hugeyurt.util.HttpComponentUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletInputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hugeyurt.po.AccessToken;
import com.hugeyurt.po.WeiXinMsg;
import com.hugeyurt.service.WeiXinService;

import java.io.IOException;


@Controller
public class WeiXinController
{
   @Resource
   private WeiXinService  weiXinService;
    @Autowired
    private ObjectMapper objectMapper;
	@RequestMapping("/weixin")
   @ResponseBody
  public String weixinChannel(HttpServletRequest request,
							 HttpServletResponse response)
  {
		
		String echostr=request.getParameter("echostr");
		System.out.println(echostr);
		/*
		 * 该if语句的作用是判断是否是接入确认消息
		 */
		if(echostr!=null)
		{
		  String time=request.getParameter("timestamp");
		  String nonce=request.getParameter("nonce");
		  String token="njupt";
		  String signature=request.getParameter("signature");
		  return echostr;
		
		}
		else {
		/*
		 * 以下部分是正常的通信信息
		 */
	       System.out.println("begin accept xml...........");
		   String xml=toString(request);
	    	System.out.println(xml);
		   WeiXinMsg msg=weiXinService.getMsg(xml);
		  
	   switch(msg.getMsgType())
		{
		  case  "text":   return weiXinService.Text2Xml(msg);
		  case  "event" :  return null;
		}
			
	
	
	      return "";
		}
	  
  }

	public static String toString(jakarta.servlet.http.HttpServletRequest request){
		  
        ServletInputStream sis = null;
        String xmlData = null;  
		try {  
              // 取HTTP请求流  
            sis = request.getInputStream();  
            // 取HTTP请求流长度  
            int size = request.getContentLength();  
            // 用于缓存每次读取的数据  
            byte[] buffer = new byte[size];  
            // 用于存放结果的数组  
            byte[] xmldataByte = new byte[size];  
            int count = 0;  
            int rbyte = 0;  
            // 循环读取  
            while (count < size) {   
                // 每次实际读取长度存于rbyte中  
                rbyte = sis.read(buffer);   
                for(int i=0;i<rbyte;i++) {  
                    xmldataByte[count + i] = buffer[i];  
                }  
                count += rbyte;  
            }  
              
            xmlData = new String(xmldataByte, "UTF-8");
     		}catch(Exception ex){
			  ex.printStackTrace();
			//return null;
		}finally{
			
		}
	
		return xmlData;
	}
	
	@RequestMapping("token.do")
	@ResponseBody
	public AccessToken  getToken() throws IOException {
	  String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxf249aab55f28777b&secret=f87f9462e8f3eb96f69bfe27a61cec8a";
	  String json= HttpComponentUtil.get(url);
	  AccessToken token=(AccessToken) objectMapper.readValue(json,AccessToken.class);
	  token.setStartTime(System.currentTimeMillis());
	    return token;

	}

}
